package com.zhangyu.shadow.repair.manager;

import com.zhangyu.shadow.repair.config.ModuleConfig;
import com.zhangyu.shadow.repair.config.SpKey;
import com.zhangyu.shadow.repair.delegate.bean.BusBean;
import com.zhangyu.shadow.repair.delegate.bean.BusHistoryBean;

import java.util.ArrayList;
import java.util.WeakHashMap;

import st.lib.base.storage.ShadowPreference;
import st.lib.base.util.utilcode.util.SPUtils;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/5
 */
public class AccountManager {

    public static final String TYPE_MAINTAIN = "maintain";
    public static final String TYPE_REPAIR = "repair";
    private String mAuthKey;
    private String mWorkCode;
    private String mSourceType;
    private String mWorkId;
    private String mCanEditTime;
    private String mWorkName;
    private String mDeptName;
    private String mGroupName;
    private ACCOUNT_TYPE mAccountType;
    private String mDepId;
    private String mGroupId;
    private String mOrgId;
    private WeakHashMap<String, Object> mBaseInfos;
    private ArrayList<BusHistoryBean> mBusHistoryBeans;

    public enum ACCOUNT_TYPE {
        MAINTAIN, REPAIR
    }


    private AccountManager() {
    }

    private static final class Holder {
        private static final AccountManager INSTANCE = new AccountManager();
    }

    public static AccountManager getInstance() {
        return Holder.INSTANCE;
    }

    /**
     * 本地是否缓存了组织关系
     *
     * @return
     */
    public boolean hasDeptOrg() {
        return ShadowPreference.getAppFlag(SpKey.KEY_DEPT_ORG);
    }

    public String getAuthKey() {
        if (mAuthKey == null) {
            mAuthKey = ShadowPreference.getCustomAppProfile(SpKey.KEY_AUTH_KEY);
        }
        return mAuthKey;
    }

    public String getWorkCode() {
        if (mWorkCode == null) {
            mWorkCode = ShadowPreference.getCustomAppProfile(SpKey.KEY_WORK_CODE);
        }
        return mWorkCode;
    }

    public AccountManager setWorkCode(String workCode) {
        ShadowPreference.addCustomAppProfile(SpKey.KEY_WORK_CODE, workCode);
        mWorkCode = workCode;
        return this;
    }

    public AccountManager setCanEditTime(String can) {
        ShadowPreference.addCustomAppProfile(SpKey.KEY_CAN_EDIT_TIME, can);
        mCanEditTime = can;
        return this;
    }

    public String getCanEditTime() {
        if (mCanEditTime == null) {
            mCanEditTime = ShadowPreference.getCustomAppProfile(SpKey.KEY_WORK_CODE,
                    "true");
        }
        return mCanEditTime;
    }


    public AccountManager setAuthKey(String authKey) {
        ShadowPreference.addCustomAppProfile(SpKey.KEY_AUTH_KEY, authKey);
        mAuthKey = authKey;
        return this;
    }

    public AccountManager setSourceType(String sourceType) {
        ShadowPreference.addCustomAppProfile(SpKey.KEY_SOURCE_TYPE, sourceType);
        mSourceType = sourceType;
        return this;
    }

    public String getSourceType() {
        if (mSourceType == null) {
            mSourceType = ShadowPreference.getCustomAppProfile(SpKey.KEY_SOURCE_TYPE);
        }
        return mSourceType;
    }

    public String getWorkId() {
        if (mWorkId == null) {
            mWorkId = ShadowPreference.getCustomAppProfile(SpKey.KEY_WORK_ID);
        }
        return mWorkId;
    }

    public AccountManager setWorkId(String workId) {
        ShadowPreference.addCustomAppProfile(SpKey.KEY_WORK_ID, workId);
        mWorkId = workId;
        return this;
    }

    public String getDeptName() {
        if (mDeptName == null) {
            mDeptName = ShadowPreference.getCustomAppProfile(SpKey.KEY_DEPT_NAME);
        }
        return mDeptName;
    }

    public AccountManager setDeptName(String deptName) {
        ShadowPreference.addCustomAppProfile(SpKey.KEY_DEPT_NAME, deptName);
        mDeptName = deptName;
        return this;
    }

    public AccountManager setOrgId(String orgId) {
        ShadowPreference.addCustomAppProfile(SpKey.KEY_ORG_ID, orgId);
        mOrgId = orgId;
        return this;
    }

    public String getOrgId() {
        if (mOrgId == null) {
            mOrgId = ShadowPreference.getCustomAppProfile(SpKey.KEY_ORG_ID);
        }
        return mOrgId;
    }

    public String getGroupName() {
        if (mGroupName == null) {
            mGroupName = ShadowPreference.getCustomAppProfile(SpKey.KEY_GROUP_NAME);
        }
        return mGroupName;
    }

    public AccountManager setGroupName(String groupName) {
        ShadowPreference.addCustomAppProfile(SpKey.KEY_GROUP_NAME, groupName);
        mGroupName = groupName;
        return this;
    }

    public String getWorkName() {
        if (mWorkName == null) {
            mWorkName = ShadowPreference.getCustomAppProfile(SpKey.KEY_WORK_NAME);
        }
        return mWorkName;
    }

    public AccountManager setWorkName(String workName) {
        ShadowPreference.addCustomAppProfile(SpKey.KEY_WORK_NAME, workName);
        mWorkName = workName;
        return this;
    }

    public String getAppId() {
        return ModuleConfig.APP_ID;
    }

    public String getDepId() {
        if (mDepId == null) {
            mDepId = ShadowPreference.getCustomAppProfile(SpKey.KEY_DEPT_ID);
        }
        return mDepId;
    }

    public AccountManager setDepId(String depId) {
        ShadowPreference.addCustomAppProfile(SpKey.KEY_DEPT_ID, depId);
        mDepId = depId;
        return this;
    }

    public String getGroupId() {
        if (mGroupId == null) {
            mGroupId = ShadowPreference.getCustomAppProfile(SpKey.KEY_GROUP_ID);
        }
        return mGroupId;
    }

    public AccountManager setGroupId(String groupId) {
        ShadowPreference.addCustomAppProfile(SpKey.KEY_GROUP_ID, groupId);
        mGroupId = groupId;
        return this;
    }

    public ACCOUNT_TYPE getAccountType() {
        if (mAccountType == null) {
            String value = ShadowPreference.getCustomAppProfile(SpKey.KEY_ACCOUNT_TYPE);
            if (value.isEmpty() || value.equals(TYPE_MAINTAIN)) {
                mAccountType = ACCOUNT_TYPE.MAINTAIN;
            } else {
                mAccountType = ACCOUNT_TYPE.REPAIR;
            }
        }
        return mAccountType;
    }

    public void addRepairBus(int type, BusBean busBean) {
        if (mBusHistoryBeans == null) {
            mBusHistoryBeans = new ArrayList<>();
        }
        mBusHistoryBeans.add(new BusHistoryBean(busBean.getId(), type, busBean.getVehicleCode(),
                busBean.getVehicleName(), busBean.getInFactoryTime()));
        SPUtils.getInstance().putObject(SpKey.KET_HISTORY_REPAIR_BUS, mBusHistoryBeans);
    }

    public ArrayList<BusHistoryBean> getRepairBus() {
        if (mBusHistoryBeans == null) {
            //noinspection unchecked
            mBusHistoryBeans = (ArrayList<BusHistoryBean>) SPUtils.getInstance()
                    .getObject(SpKey.KET_HISTORY_REPAIR_BUS);
        }
        return mBusHistoryBeans;
    }

    public void removeBus(BusHistoryBean busHistoryBean) {
        if (mBusHistoryBeans != null) {
            mBusHistoryBeans.remove(busHistoryBean);
            SPUtils.getInstance().putObject(SpKey.KET_HISTORY_REPAIR_BUS, mBusHistoryBeans);
        }
    }

    public void clearRepairHistory() {
        if (mBusHistoryBeans != null) {
            mBusHistoryBeans.clear();
            SPUtils.getInstance().remove(SpKey.KET_HISTORY_REPAIR_BUS);
        }
    }

    public void setAccountType(ACCOUNT_TYPE accountType) {
        if (accountType != null) {
            ShadowPreference.addCustomAppProfile(SpKey.KEY_ACCOUNT_TYPE,
                    (accountType == ACCOUNT_TYPE.MAINTAIN) ? TYPE_MAINTAIN : TYPE_REPAIR);
            mAccountType = accountType;
        }

    }

    public void resetOrg() {
        mDeptName = null;
        mGroupName = null;
        mDepId = null;
        mGroupId = null;
        ShadowPreference.addCustomAppProfile(SpKey.KEY_DEPT_NAME, "");
        ShadowPreference.addCustomAppProfile(SpKey.KEY_DEPT_ID, "");
        ShadowPreference.addCustomAppProfile(SpKey.KEY_GROUP_NAME, "");
        ShadowPreference.addCustomAppProfile(SpKey.KEY_GROUP_ID, "");
        ShadowPreference.setAppFlag(SpKey.KEY_DEPT_ORG, false);
    }

    public WeakHashMap<String, Object> getBaseInfo() {
        if (mBaseInfos == null) {
            mBaseInfos = new WeakHashMap<>();
        }
        mBaseInfos.put("appId", getAppId());
        mBaseInfos.put("authKey", getAuthKey());
        mBaseInfos.put("workCode", getWorkCode());
        mBaseInfos.put("sourceType", getSourceType());
        return mBaseInfos;
    }
}
